//
//  CounterView.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/14.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "CounterView.h"


@interface DisablePlasteTF : UITextField

@end
@implementation DisablePlasteTF
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    UIMenuController*menuController = [UIMenuController sharedMenuController];
    if(menuController) {
        [UIMenuController sharedMenuController].menuVisible=NO;
    }
    return NO;
}

@end

@interface CounterView()<UITextFieldDelegate>
{
    float  _interestV;
    NSInteger _days;
}
@property (nonatomic,strong) UILabel *titleLb;

@property (nonatomic,strong) UIView *linewView;

@property (nonatomic,strong) UIImageView *hideView;

@property (nonatomic,strong) DisablePlasteTF *mainTf;

@property (nonatomic,strong) UILabel *desLb;

@property (nonatomic,strong) UILabel *incomelb;

@property (nonatomic,strong) HidenBlock block;

@end

@implementation CounterView
- (instancetype)initWithBlock:(HidenBlock)block{
    
    if ([super init]) {
        [self configUI];
        _block = block;
    }
    return self;
}
- (void)setCalculateProVider:(float)interestV days:(NSInteger)days{
    _interestV = interestV;
    _days = days;
}
- (void)configUI{
    self.userInteractionEnabled = YES;
    self.clipsToBounds = YES;
    self.layer.cornerRadius = 2;
    self.backgroundColor = RGBCOLOR(255, 255, 255);
    [self addSubview:self.titleLb];
    [self addSubview:self.hideView];
    [self addSubview:self.linewView];
    [self addSubview:self.mainTf];
    [self addSubview:self.desLb];
    [self addSubview:self.incomelb];
}
- (void)setFirstRespond{
    [_mainTf becomeFirstResponder];
}
- (void)tap{
    if (_block) {
        [self.mainTf endEditing:YES];
        _block();
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (str.length>13) {
        return NO;
    }
   self.incomelb.text = [self calByStr:str];
    
    return YES;
}
- (NSString *)calByStr:(NSString *)firCount{
    float x = [firCount floatValue]/365/100;
    return   [NSString stringWithFormat:@"%.2f",x*_interestV*_days];
}
- (void)layoutSubviews{
    CGFloat gap = 15;
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self);
        make.top.equalTo(self);
        make.height.equalTo(@44);
        make.left.equalTo(@0);
    }];
    [_hideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(26, 26));
        make.centerX.equalTo(self.mas_right).offset(-25);
        make.centerY.equalTo(self.mas_top).offset(25);
    }];
    [_linewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self);
        make.top.equalTo(_titleLb.mas_bottom);
        make.height.equalTo(@0.4);
        make.left.equalTo(@0);
    }];
    [_mainTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_linewView.mas_bottom).offset(gap);
        make.centerX.equalTo(self);
        make.width.equalTo(self.mas_width).offset(-gap*2);
        make.height.equalTo(@44);
    }];
    [_desLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mainTf);
        make.top.equalTo(_mainTf.mas_bottom).offset(gap);
        make.height.equalTo(@12);
        make.width.equalTo(@(12*9));
    }];
    [_incomelb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_mainTf);
        make.centerY.equalTo(_desLb);
        make.height.equalTo(@16);
        make.width.equalTo(@(KWidth));
    }];
}
- (UILabel *)titleLb{
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.font = [UIFont systemFontOfSize:15];
        _titleLb.text = @"计算收益";
        _titleLb.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLb;
}
- (UILabel *)desLb{
    if (!_desLb) {
        _desLb = [UILabel new];
        _desLb.font = [UIFont systemFontOfSize:13];
        _desLb.textColor = RGBCOLOR(155, 154, 155);
        _desLb.text = @"预计收益(元)";
    }
    return _desLb;
}
- (UILabel *)incomelb{
    if (!_incomelb) {
        _incomelb = [UILabel new];
        _incomelb.textAlignment = NSTextAlignmentRight;
        _incomelb.textColor = ZZGoldColor;
        _incomelb.font = [UIFont systemFontOfSize:15];
        _incomelb.text = @"0.00";
    }
    return _incomelb;
}
- (UIView *)linewView{
    if (!_linewView) {
        _linewView = [UIView new];
        _linewView.backgroundColor = RGBCOLOR(228, 228, 228);
    }
    return _linewView;
}
- (DisablePlasteTF *)mainTf{
    if (!_mainTf) {
        _mainTf = [[DisablePlasteTF alloc]init];
//        _mainTf.maximumTextLength = 10;
        _mainTf.backgroundColor = RGBCOLOR(245, 245, 245);
        _mainTf.textColor = RGBCOLOR(155, 154, 155);
        _mainTf.font = [UIFont systemFontOfSize:13];
        _mainTf.textAlignment = NSTextAlignmentCenter;
        _mainTf.keyboardType = UIKeyboardTypeNumberPad;
        _mainTf.placeholder = @"投资金额(元)";
        _mainTf.delegate = self;
    }
    return _mainTf;
}
- (UIImageView *)hideView{
    if (!_hideView) {
        _hideView = [UIImageView new];
        _hideView.userInteractionEnabled = YES;
        _hideView.image = UIImageMake(@"dismissicon");
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [_hideView addGestureRecognizer:tap];
//        _hideView.backgroundColor = RGBCOLOR(217, 217, 217);
    }
    return _hideView;
}

@end
